package com.njtoyo.taxi.admin.service.marketing.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.google.common.collect.Lists;
import com.njtoyo.taxi.admin.library.Const;
import com.njtoyo.taxi.admin.mapper.master.entity.ActivityPrized;
import com.njtoyo.taxi.admin.mapper.master.enums.ActivityPrizedCategory;
import com.njtoyo.taxi.admin.mapper.master.mapper.ActivityPrizedMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.ActivitySeckillRecordMapper;
import com.njtoyo.taxi.admin.rest.presenter.marketing.seckill.RecordListPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.marketing.seckill.RecordListWrapper;
import com.njtoyo.taxi.admin.service.marketing.SeckillRecordService;
import com.taxi.entity.common.RestResult;
import com.taxi.entity.common.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@DS(Const.PRIMARY_DS)
public class SeckillRecordServiceImpl implements SeckillRecordService {

    @Autowired
    private ActivityPrizedMapper activityPrizedMapper;

    @Autowired
    private ActivitySeckillRecordMapper activitySeckillRecordMapper;

    @Override
    public RestResult getList(Long activityId, RecordListWrapper wrapper) {
        // 校验秒杀活动是否存在
        ActivityPrized seckill = activityPrizedMapper.getActivityById(ActivityPrizedCategory.seckill, activityId);
        if (Objects.isNull(seckill)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }

        wrapper.setActivityId(activityId);
        wrapper.setOrders(Lists.newArrayList(new OrderItem("id", false)));

        IPage<RecordListPresenter> res = activitySeckillRecordMapper.getList(wrapper);

        return RestResult.success(res);
    }

}
